package hredis

import (
	"context"
	"fmt"
	"ipv6am/global"
	"time"

	"github.com/gogf/gf/v2/util/gconv"
)

// 缓存函数
// 参数1: key
// 参数2: val
// 参数3: 缓存时间/秒
func S(any ...interface{}) string {

	var (
		client = global.RedisDB
		ctx    = context.Background()
		len    = len(any)
	)

	// 获取值
	if len == 1 {
		var (
			key = fmt.Sprint(any[0])
		)

		val, _ := client.Get(ctx, key).Result()
		return val
	}

	// 设置值
	if len == 3 {
		var (
			key    = fmt.Sprint(any[0])
			val    = gconv.String(any[1])
			expire = time.Duration(gconv.Int(any[2])) * time.Second
		)

		_, _ = client.Set(ctx, key, val, expire).Result() // 0 表示没有过期时间
		return val
	}

	return ""
}
